﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ADC.DAL;

namespace ADC.BL
{

    public class  AboutUsItem
    {
        public string Name { get; set; }
        public string Title { get; set; }
        public int AboutUsId { get; set; }
        public string Description { get; set; }
        public DateTime ?ModifiedDate { get; set; }
    }

    public class AboutUsFactory
    {
        public static List<AboutUsItem> GetAll()
        {
            var db = new dbADCEntities();
            List<AboutUsItem> listAboutUs = (from u in db.Users
                                             join aboutUs in db.AboutUs on u.UserID equals aboutUs.ModifiedByID
                       select new AboutUsItem()
                                  {
                                      Name = u.Name,
                                      Title = aboutUs.Title,
                                      AboutUsId = aboutUs.AboutUsID,
                                      Description = aboutUs.Description,
                                      ModifiedDate = aboutUs.ModifiedDate
                                  }).ToList();                        
            return listAboutUs;
        }

        /// <summary>
        /// Get by ID
        /// </summary>
        /// <param name="aboutUsID"></param>
        /// <returns></returns>
        public static AboutU GetByID(int aboutUsID)
        {
            try
            {
                var db = new dbADCEntities();
                return db.AboutUs.Where(g => g.AboutUsID == aboutUsID).FirstOrDefault();
            }
            catch (Exception)
            {
                // Log here
                throw;
            }
        }

        public static int Update(AboutU aboutU)
        {
            try
            {
                var db = new dbADCEntities();
                var objAboutU = new AboutU();
                objAboutU = db.AboutUs.Where(g => g.AboutUsID == aboutU.AboutUsID).FirstOrDefault();

                objAboutU.Title = aboutU.Title;
                objAboutU.Description = aboutU.Description;
                objAboutU.ModifiedDate = DateTime.Now;
                //TODO: 
                //aboutU.CreatedByID = aboutU.CreatedByID;
                return db.SaveChanges();
            }
            catch (Exception)
            {
                return -1;
                // Log here
                throw;
            }
            
        }

    }
}
